package connect;



/*
 * DBUtil.java
 *
 * Created on August 22, 2008, 8:02 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 *
 * @author Administrator
 */
public class connect {
    
    private final String server = "localhost";
    private final int port = 1433;
    private final String databaseName = "prime";
    private final String username = "sa";
    private final String password = "123";
    Connection con = null;
    public connect() {
    }
    
    public Connection connect() {
        
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        } catch(ClassNotFoundException e) {
            System.out.println("Khong tim thay Driver cho SQL Server 2005");
        }
        try {
            con = DriverManager.getConnection(getURLConnection());
        } catch(SQLException e) {
            System.out.println("Khong ket noi duoc voi database");
        }
        return con;
    }
    private  String getURLConnection() {
        return "jdbc:sqlserver://" + server + ":" + port +
                ";databaseName=" + databaseName +
                ";User=" + username +
                ";Password=" + password;
    }
    public ResultSet executeSQL(String querySQL) throws SQLException{
        Statement s = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
        ResultSet rs = s.executeQuery(querySQL);
        return rs;
    }
    public int executeUpdate(String SQL) throws SQLException{
        Statement s=con.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_UPDATABLE);
        int kq=s.executeUpdate(SQL);
        return kq;
    }

}
